<template>
  <uni-popup ref="popup" type="center" @maskClick="close">
    <view class="content">
      <uni-easyinput placeholder="请输入目录(默认Downloads目录)" v-model="uploadPath"></uni-easyinput>
      <view class="upload-path-box" v-if="tempFilePaths">
        <view class="upload-file">
          <view class="upload-top">
            <text>{{ tempFilesName }}</text>
            <uni-icons style="width: 30px" type="close" size="25" color="#ccc" @click="clearFileInfo"></uni-icons>
          </view>

          <view class="progress-box">
            <view class="progress-bar j-shine" :style="{ width: progress + '%' }"></view>
          </view>
        </view>
        <button v-if="completeUpload" class="upload-btn base-button ibiza-sunset" @click="clearFileInfo">再次上传</button>
        <button v-else class="upload-btn base-button" @click="uploadFile">上传</button>
      </view>
      <view class="choose-file-box" v-else @click="chooseFile">
        <uni-icons type="plusempty" size="20" color="#ccc"></uni-icons>
        <text>选择文件</text>
      </view>
      <uni-icons class="close-btn" type="close" size="50" color="#eee" @click="close"></uni-icons>
    </view>
  </uni-popup>
</template>

<script setup>
import { ref, defineProps } from "vue";

const props = defineProps({
  computerInfo: {
    type: Object,
  },
});

const tempFilePaths = ref("");
const tempFilesName = ref("");
const uploadPath = ref("");
const progress = ref(0);
const completeUpload = ref(false);

// 弹窗示例
const popup = ref();
const open = () => {
  clearFileInfo();
  popup.value.open();
};

const close = () => {
  popup.value.close();
};

// 清空数据重新上传
const clearFileInfo = () => {
  tempFilePaths.value = "";
  tempFilesName.value = "";
  progress.value = 0;
  completeUpload.value = false;
};
// 选择文件
const chooseFile = () => {
  uni.chooseFile({
    count: 1,
    success: (res) => {
      tempFilePaths.value = res.tempFilePaths[0];
      tempFilesName.value = res.tempFiles[0].name;
    },
    fail: () => {
      uni.showToast({
        title: "文件选择失败",
        icon: "none",
      });
    },
  });
};

// 上传文件
const uploadFile = () => {
  let errorTip = (msg) => {
    return uni.showToast({
      title: msg,
      icon: "error",
    });
  };
  let uploadTask = uni.uploadFile({
    url: `http://${props.computerInfo.ip}:${props.computerInfo.port}/file/upload`, //仅为示例，非真实的接口地址
    filePath: tempFilePaths.value,
    name: "file",
    header: {
      "X-Access-Key": props.computerInfo.key,
    },
    formData: {
      path: uploadPath.value,
    },
    success: (uploadFileRes) => {
      let uploadFileData = JSON.parse(uploadFileRes.data);
      if (uploadFileRes.statusCode === 200) {
        if (uploadFileData.code === 200) {
          completeUpload.value = true;
          uni.showToast({
            title: "上传成功",
            icon: "success",
          });
        } else {
          errorTip(uploadFileData.msg);
        }
      } else {
        errorTip(uploadFileData.msg);
      }
    },
    fail: () => {
      errorTip("上传失败");
    },
  });
  uploadTask.onProgressUpdate((res) => {
    progress.value = res.progress;
  });
};

defineExpose({ open, close });
</script>

<style lang="scss" scoped>
.content {
  width: 300px;
  height: 180px;
  box-sizing: border-box;
  background-color: #ffffff;
  padding: 20px;
  border-radius: 5px;
  .base-box {
    height: 50px;
    width: 100%;
  }
  .upload-path-box {
    .upload-file {
      @extend .base-box;
      border: 1px solid #ccc;
      border-radius: 5px;
      margin-top: 10px;
      box-sizing: border-box;
      padding: 5px 10px;
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      .upload-top {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
      text {
        display: block;
        width: 100%;
        color: #4f4f4f;
        font-size: 15px;
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
      }
    }
    .progress-box {
      background-color: #f3f3f3;
      height: 5px;
      border-radius: 3px;
      .progress-bar {
        height: 5px;
        border-radius: 3px;
      }
    }
    .upload-btn {
      margin-top: 10px;
    }
  }
  .choose-file-box {
    @extend .base-box;
    border: 1px dashed #ccc;
    border-radius: 5px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 10px;
    text {
      color: #ccc;
      margin-right: 5px;
    }
  }
  .close-btn {
    position: absolute;
    bottom: -70px;
    left: 50%;
    transform: translateX(-50%);
  }
}
</style>
